
function UnitTests:TestKeyLocationsReductionAfterDoorCollapse()

	-- test deadlock filtering when door collpase : some deadlock found and some doors forced to collapse open
	
	local stage =
	{
		-- we do a grid of 9 rooms as this :
		--
		--   ----------|----------|----------
		--  |          |                     |
		--	|    R1    |    R2         R3    |
		--	|          |                     |
		--  |          |                     |
		--	|---|14|---|---|25|---|---|36|---|
		--  |          |          -          |
		--	|    R4    |    R5    5    R6    |
		--	|          |          6          |
		--  |          |          -          |
		--  |-        -|----------|          |
		--  |          -                     |
		--	|    R7    7    R8         R9    |
		--	|          8   PLY               |
		--  |          -                     |
		--	 ----------|----------|----------
		--
		-- The test scenario is as folow :
		--
		-- All doors are biased to collapse as closed. Only the dead lockfiltering can force them to collapse open
		-- It must be noted that when the player goes from room to room, it doesn't open the doors on his passage, 
		-- to artificially complicate the work of the deadlock filtering.
		--
		-- Player starts in R8. D78 will collapse closed. K78 is removed from R7, R4, R1, R2, R3, R5....
		--
		-- We open D36
		--
		-- Player goes to R6. 
		-- D56 collapse close. K56 is not acessible in R5, R1, R4, R7
		
		Rooms =
		{
			Room1 =
			{			
			},
			
			Room2 =
			{	
			},
			
			Room3 =
			{	
			},
			
			Room4 =
			{	
			},								
			
			Room5 =
			{	
			},								
			
			Room6 =
			{	
			},								
			
			Room7 =
			{	
			},								
			
			Room8 =
			{	
			},								
			
			Room9 =
			{	
			},								
		},
		
		
		Doors =
		{
			{
				Rooms = 
				{
					"Room1",
					"Room4"
				},
				
				ClosedAndLocked = true	
			},
			
			{
				Rooms = 
				{
					"Room2",
					"Room5"
				},
				
				ClosedAndLocked = true		
			},
			
			{
				Rooms = 
				{
					"Room3",
					"Room6"
				},
				
				ClosedAndLocked = true		
			},
			
			{
				Rooms = 
				{
					"Room5",
					"Room6"
				},
				
				ClosedAndLocked = true		
			},
			
			{
				Rooms = 
				{
					"Room7",
					"Room8"
				},
				
				ClosedAndLocked = true		
			},
						
			{
				Rooms = 
				{
					"Room2",
					"Room3"
				},
			},			
			
			{
				Rooms = 
				{
					"Room4",
					"Room7"
				},
			},			
			
			{
				Rooms = 
				{
					"Room6",
					"Room9"
				},
			},
			
			{
				Rooms = 
				{
					"Room8",
					"Room9"
				},
			},			
		}		
	}
	
	local cast =
	{
		Player =
		{
			initialLocation = "Room8"
		},
	}	

	local game = Game:new(stage, cast)
	
	local room1 = gField.entityTypes.Location.Room1
	local room2 = gField.entityTypes.Location.Room2
	local room3 = gField.entityTypes.Location.Room3
	local room4 = gField.entityTypes.Location.Room4
	local room5 = gField.entityTypes.Location.Room5
	local room6 = gField.entityTypes.Location.Room6
	local room7 = gField.entityTypes.Location.Room7
	local room8 = gField.entityTypes.Location.Room8
	local room9 = gField.entityTypes.Location.Room9
	
	local key14 = gField.entityTypes.Key["Room1<->Room4"]
	local key25 = gField.entityTypes.Key["Room2<->Room5"]
	local key36 = gField.entityTypes.Key["Room3<->Room6"]
	local key56 = gField.entityTypes.Key["Room5<->Room6"]
	local key78 = gField.entityTypes.Key["Room7<->Room8"]
	
	local door14 = gField.entityTypes.Door["Room1<->Room4"]
	local door25 = gField.entityTypes.Door["Room2<->Room5"]
	local door36 = gField.entityTypes.Door["Room3<->Room6"]
	local door56 = gField.entityTypes.Door["Room5<->Room6"]
	local door78 = gField.entityTypes.Door["Room7<->Room8"]

	
	-- all doors biased to collapse closed
	 
	gRandom:addContextualBias({"Door", "Room1<->Room4", "EnablingSteps"}, Door.LockedStep)
	gRandom:addContextualBias({"Door", "Room2<->Room5", "EnablingSteps"}, Door.LockedStep)
	gRandom:addContextualBias({"Door", "Room3<->Room6", "EnablingSteps"}, Door.LockedStep)
	gRandom:addContextualBias({"Door", "Room5<->Room6", "EnablingSteps"}, Door.LockedStep)
	gRandom:addContextualBias({"Door", "Room6<->Room9", "EnablingSteps"}, Door.LockedStep)
	gRandom:addContextualBias({"Door", "Room7<->Room8", "EnablingSteps"}, Door.LockedStep)


	-- Key location bias
	
	gRandom:addContextualBias({"Key", "Room7<->Room8", "CurrentContainer"}, room6)
	gRandom:addContextualBias({"Key", "Room5<->Room6", "CurrentContainer"}, room9)
	gRandom:addContextualBias({"Key", "Room3<->Room6", "CurrentContainer"}, room3)
	gRandom:addContextualBias({"Key", "Room1<->Room4", "CurrentContainer"}, room1)
	gRandom:addContextualBias({"Key", "Room2<->Room5", "CurrentContainer"}, room5)


	-- start test scenario
		
	gGame:start()
	
	assert(not door78:isEnabled(true))

	assert(not key78.CurrentContainer[room7])	-- locations removed by deadlock filter
	assert(not key78.CurrentContainer[room4])
	assert(not key78.CurrentContainer[room1])
	assert(not key78.CurrentContainer[room2])
	assert(not key78.CurrentContainer[room3])
	assert(not key78.CurrentContainer[room5])
	
	assert(not key78.CurrentContainer[room8])	-- location removed because room is observed and key is biased toward room6
	assert(key78.CurrentContainer[room9])
	assert(key78.CurrentContainer[room6])
	
	door36:enable()
	gGame:onBeat()		
	
	gPlayer.body:execute("moveTo", room6)
	gGame:onBeat()

	assert(not key56.CurrentContainer[room5])	-- locations removed by deadlock filter
	assert(not key56.CurrentContainer[room1])
	assert(not key56.CurrentContainer[room4])
	assert(not key56.CurrentContainer[room7])

	assert(not key56.CurrentContainer[room6])	-- locations removed because room is observed and key is biased toward room9
	assert(not key56.CurrentContainer[room8])	-- observed not there at previos beat

	assert(key56.CurrentContainer[room2])
	assert(key56.CurrentContainer[room3])
	assert(key56.CurrentContainer[room9])
end